/**
 * @class   wosh::persistence::plaintext::EncoderProtocolPlainText
 * @brief   
 *
 ****************************************************************************
 * @version 0.8.399 $Id: EncoderProtocolPlainText.h 2657 2010-06-15 19:57:29Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Persistence_PlainText_EncoderProtocolPlainText_H__
 #define __WOSH_Persistence_PlainText_EncoderProtocolPlainText_H__

 #include <core/EncoderProtocol.h>
 #include <core/ObjectFactory.h>
 #include <ostream>
 #include <istream>


using namespace wosh;
namespace wosh {
 class PersistenceDb;
 namespace persistence {
  namespace plaintext {


#undef WOSH_ENCODER_PlainText_CLASS
#define WOSH_ENCODER_PlainText_CLASS(CLASSNAME, OBJECT_TYPE)		\
	class CLASSNAME : public wosh::persistence::plaintext::EncoderBasePlainText {											\
		public: virtual bool isAbstract() const { return false; }															\
		public: static const char* className() { return "wosh::persistence::plaintext::" #CLASSNAME; }						\
		public: virtual inline const char* getClassName() const { return className(); }										\
		public: static const char* mappedObject() { return #OBJECT_TYPE; }													\
		public: virtual inline const char* getMappedObject() const { return mappedObject(); }								\
		public: inline WRESULT serializeTo( const ISerializable* object_s, std::ostream& output_stream, int encoder_options ) {	\
			return CLASSNAME::serializeTo( dynamic_cast<const OBJECT_TYPE*>(object_s), output_stream, encoder_options); }			\
		public: static WRESULT serializeTo( const OBJECT_TYPE* object, std::ostream& output_stream, int encoder_options );			\
		public: CLASSNAME() : EncoderBasePlainText() { }																	\
	};

#undef WOSH_Encoder_PlainText_BIND
#define WOSH_Encoder_PlainText_BIND(ENCODER_CLASS, OBJECT_CLASSNAME, VERSION, TMP ) \
	WOSH_ENCODER_BIND(wosh::persistence::plaintext::EncoderProtocolPlainText, ENCODER_CLASS, OBJECT_CLASSNAME, VERSION, TMP )



class EncoderBasePlainText : public EncoderBase
 {
	WOSH_CLASSNAME(wosh::persistence::plaintext::EncoderProtocolPlainText)
	WOSH_CLASS_ABSTRACT(true)

	public:
		virtual WRESULT serializeTo( const ISerializable* object, std::ostream& output_stream, int encoder_options ) = 0;
		virtual WRESULT deserializeFrom( ISerializable* , std::istream& ) {
			return WRET_NOT_IMPLEMENTED;
		 }

	public:
		virtual int64 serializeTo( const ISerializable* object, char* buffer, int64 buffer_size, int encoder_options );
		virtual int64 deserializeFrom( ISerializable* object, const char* buffer, int64 buffer_size );

	public:
		static unsigned int evalStringSize( const wosh::ISerializable* object, unsigned int maxsize = LIMIT_INT_MAX );

		template<typename T>
		static void serializeVector( const std::vector<T>& dataset, std::ostream& output_stream, const std::string& item_separator = "\r\n" ) {
			typename std::vector<T>::const_iterator it;
			const typename std::vector<T>::const_iterator it_end = dataset.end();
			for ( it=dataset.begin(); it!=it_end; ++it ) {
				output_stream << *it;
				output_stream << item_separator;
			 }
		 }

		template<typename T, typename Y>
		static void serializeMap( const std::map<T, Y>& dataset, std::ostream& output_stream, const std::string& item_separator = "\r\n", const std::string& pair_separator = "\t" ) {
			typename std::map<T, Y>::const_iterator it;
			const typename std::map<T, Y>::const_iterator it_end = dataset.end();
			for ( it=dataset.begin(); it!=it_end; ++it ) {
				output_stream << it->first;
				output_stream << pair_separator;
				output_stream << it->second;
				output_stream << item_separator;
			 }
		 }

	public:
		EncoderBasePlainText() : EncoderBase() { }
		virtual ~EncoderBasePlainText() { }

};


// ------------------------------------------------------------------------------------------------------------------------

class PersistenceDbXmlPlainText;

class EncoderProtocolPlainText : public EncoderProtocol
 {
	WOSH_ENCODERPROTOCOL(wosh::persistence::plaintext::EncoderProtocolPlainText)

	public:
		EncoderProtocolPlainText();
		virtual ~EncoderProtocolPlainText() { }


	public:
		const char* getProtocol() const				{ return "PLAINTEXT::DEFAULT"; }
		double getProtocolVersion() const			{ return 0.0; }

	public:
		inline PersistenceDb* createDb() const		{ return NULL; }

		virtual WRESULT saveObject( const ISerializable* , const std::string&  ) {
			return WRET_ERR_INTERNAL;
		 }
		virtual WRESULT saveObjects( const std::vector<ISerializable*>& , const std::string&  ) {
			return WRET_ERR_INTERNAL;
		 }
		virtual WRESULT loadObjects( std::vector<ISerializable*>& , const std::string&  ) {
			return WRET_ERR_INTERNAL;
		 }

	public:
		static WRESULT serializeTo( const ISerializable* object, std::ostream& output_stream, int encoder_options );
		static WRESULT deserializeFrom( ISerializable* object, std::istream& input_stream );
		static ISerializable* deserializeFrom( std::istream& input_stream, WRESULT* return_code = NULL );

		template <class DYNAMIC_CAST>
		static inline DYNAMIC_CAST* deserializeFrom( std::istream& input_stream, WRESULT* return_code = NULL ) {
			return dynamic_cast<DYNAMIC_CAST*>( deserializeFrom(input_stream, return_code) );
		 }


	protected:
//		static EncoderBase* getEncoderFor_( const char* classname, double version = 0.0, bool recursive = true );

 };


  }; // namespace plaintext
 }; // namespace persistence
}; // namespace wosh

#endif //__WOSH_Persistence_PlainText_EncoderProtocolPlainText_H__
